package com.sl.pmpclient.utils;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * 用于sqlite数据库操作的类
 * 
 * @author 烁灵
 * 
 */
class DataRecordOpenHelper extends SQLiteOpenHelper {
	public static String DB_NAME = "SL_DIGIPET_DB.db";
	public static int VERSION = 1;

	public DataRecordOpenHelper(Context context) {
		this(context, VERSION);
	}

	public DataRecordOpenHelper(Context context, int version) {
		this(context, DB_NAME, null, version);
	}

	public DataRecordOpenHelper(Context context, String name,
			CursorFactory factory, int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		// 创建数据库
		// 该条记录ID(自增) 用户名 加密后的密码
		String table_remembered_users = "CREATE TABLE IF NOT EXISTS remembered_users(remembered_user_id INT AUTO_INCREMENT, remembered_user_name VARCHAR(256) NOT NULL, remembered_user_psw CHAR(64) NOT NULL)";
		db.execSQL(table_remembered_users);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}
}

/**
 * 数据库操作的工具类
 * 
 * @author 烁灵
 * 
 */
public class DataRecordUtil {
	/**
	 * 删除被记住的用户
	 * 
	 * @param context
	 * @param username
	 * @return
	 */
	public static boolean deleteRememberedUser(Context context, String username) {
		DataRecordOpenHelper droh = new DataRecordOpenHelper(context);
		SQLiteDatabase db = droh.getWritableDatabase();
		db.delete("remembered_users", "remembered_user_name=?",
				new String[] { username });
		db.close();
		return true;
	}

	/**
	 * 取出所有被记住的用户
	 * 
	 * @param context
	 * @return
	 */
	public static ArrayList<ContentValues> getAllRememberedUsers(Context context) {
		DataRecordOpenHelper droh = new DataRecordOpenHelper(context);
		SQLiteDatabase db = droh.getReadableDatabase();
		Cursor cursor = db.query("remembered_users", new String[] {
				"remembered_user_id", "remembered_user_name",
				"remembered_user_psw" }, null, null, null, null,
				"remembered_user_id DESC");
		ArrayList<ContentValues> list = new ArrayList<ContentValues>();
		while (cursor.moveToNext()) { // 有数据
			ContentValues value = new ContentValues();
			value.put("remembered_user_id",
					cursor.getInt(cursor.getColumnIndex("remembered_user_id")));
			value.put("remembered_user_name", cursor.getString(cursor
					.getColumnIndex("remembered_user_name")));
			value.put("remembered_user_psw", cursor.getString(cursor
					.getColumnIndex("remembered_user_psw")));
			list.add(value);
		}
		cursor.close();
		db.close();
		return list;
	}

	/**
	 * 取出指定用户名的被记住的用户
	 * 
	 * @param context
	 * @param user_name
	 * @return
	 */
	public static ContentValues getRememberedUserPswByName(Context context,
			String user_name) {
		DataRecordOpenHelper droh = new DataRecordOpenHelper(context);
		SQLiteDatabase db = droh.getReadableDatabase();
		Cursor cursor = db.query("remembered_users", new String[] {
				"remembered_user_id", "remembered_user_name",
				"remembered_user_psw" }, "remembered_user_name=?",
				new String[] { user_name }, null, null,
				"remembered_user_id DESC");
		ContentValues value = null;
		if (cursor.moveToNext()) { // 有数据
			value = new ContentValues();
			value.put("remembered_user_id",
					cursor.getInt(cursor.getColumnIndex("remembered_user_id")));
			value.put("remembered_user_name", cursor.getString(cursor
					.getColumnIndex("remembered_user_name")));
			value.put("remembered_user_psw", cursor.getString(cursor
					.getColumnIndex("remembered_user_psw")));
		}
		cursor.close();
		db.close();
		return value;
	}

	/**
	 * 插入一个记录的用户
	 * 
	 * @param context
	 * @param username
	 * @param password
	 * @return
	 */
	public static boolean insertRememberedUser(Context context,
			String username, String password) {
		DataRecordOpenHelper droh = new DataRecordOpenHelper(context);
		SQLiteDatabase db = droh.getWritableDatabase();
		ContentValues value = new ContentValues();
		value.put("remembered_user_name", username);
		value.put("remembered_user_psw", password);
		db.insert("remembered_users", null, value);
		db.close();
		return true;
	}
}
